package com.capacitydorm.server.web;

import com.capacitydorm.server.common.vo.*;
import com.capacitydorm.server.domain.Dorm;
import com.capacitydorm.server.domain.User;
import com.capacitydorm.server.domain.wxuser;
import com.capacitydorm.server.service.WxuserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/wxuser")
public class WxuserContrpller {

        @Resource
        private WxuserService wxuserService;

        /**
         * 设置快速查询
         * @return
         */
        @PutMapping("/quicksearch")
        public ResponseEntity<Result> setCheck(wxuser wxuser){
                try{
                        wxuserService.updateQuickSeach(wxuser);
                        return new ResponseEntity<>(new Result(200),HttpStatus.OK);
                } catch (Exception e){
                        e.printStackTrace();
                        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }

        }

        /**
         *
         * 展示快速查询
         * @param id
         * @return
         */
        @GetMapping("/showQuickSeach/{id}")
        private ResponseEntity<Result> showQuickSeach(@PathVariable("id") Integer id){
                try{
                      List<QuickCheckVo> quickSeach = wxuserService.selectQuickSeach(id);
                      return new ResponseEntity<>(new Result(200,null,quickSeach),HttpStatus.OK);
                }catch(Exception e){
                        return  new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }

        /**
         * 快速查询
         * @param id
         * @return
         */
        @GetMapping("/{id}")
        private ResponseEntity<Result> quickSeach(@PathVariable("id") Integer id){
                try{
                        List<Dorm> quickSeach = wxuserService.quickSeach(id);
                        return new ResponseEntity<>(new Result(200,null,quickSeach),HttpStatus.OK);
                }catch(Exception e){
                        return  new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
        /**
         * 设置快速查询1.0
         * @param id
         * @return
         */
        @GetMapping("/alldorm/{id}")
        private ResponseEntity<Result> quickSeachByFid(@PathVariable("id") Integer id){
                try{
                        NewDormVo quickSeach = wxuserService.quickSeachAll(id);
                        return new ResponseEntity<>(new Result(200,null,quickSeach),HttpStatus.OK);
                }catch(Exception e){
                        return  new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }

        /**
         * 分页查询全部微信用户
         */
        @GetMapping("/page")
        private ResponseEntity<Result> showAllWxuser(Integer page,Integer rows){
                try {
                        ResultVo resultVo = wxuserService.selectAll(page, rows);
                        return new ResponseEntity<>(new Result(200,null,resultVo),HttpStatus.OK);
                } catch (Exception e) {
                        e.printStackTrace();
                        return  new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }

        }


        /**
         *      添加微信用户
         */
        @PostMapping
        public ResponseEntity<Result> addWxUser(WxuserVo wxuserVo){
                try {
                        wxuserService.addUser(wxuserVo);
                        return new ResponseEntity<>(new Result(201),HttpStatus.CREATED);
                } catch (Exception e) {
                        e.printStackTrace();
                        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
        /**
         *    根据id删除微信用户
         */
        @DeleteMapping("/{id}")
        public ResponseEntity<Result> delectWxuserById(@PathVariable("id") Integer id ){
                try{
                        wxuserService.delectWxuserById(id);
                        return new ResponseEntity<>(new Result(204),HttpStatus.NO_CONTENT);
                }catch (Exception e){
                        e.printStackTrace();
                        return  new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }

        /**
         *   根据id改微信用户
         */
        @PutMapping("/{id}")
        public ResponseEntity<Result> updateWxuserById(WxuserVo wxuserVo,@PathVariable("id") Integer id){
                try {
                        wxuserVo.setId(id);
                        wxuserService.updateWxuserById(wxuserVo);
                        return new ResponseEntity<>(new Result(200),HttpStatus.OK);
                }catch (Exception e){
                        e.printStackTrace();
                        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }
        /**
         *   根据id查找微信用户
         */
        @GetMapping("wxuser/{id}")
        public ResponseEntity<Result> checkWxuserById(@PathVariable("id") Integer id){
                try {
                        wxuser wxuser = wxuserService.checkWxuserById(id);
                     User user = wxuserService.selectUser(id);
                        WxuserVo wxuserVo = new WxuserVo();
                        wxuserVo.setId(wxuser.getId());
                        wxuserVo.setClasses(wxuser.getClasses());
                        wxuserVo.setName(wxuser.getName());
                        wxuserVo.setOpenId(wxuser.getOpenId());
                        wxuserVo.setPhone(wxuser.getPhone());
                        wxuserVo.setSex(wxuser.getSex());
                        wxuserVo.setPassword(user.getPassword());
                        wxuserVo.setUsername(user.getUsername());
                        return new ResponseEntity<>( new Result(200,null,wxuserVo),HttpStatus.OK);
                }catch (Exception e){
                        e.printStackTrace();
                        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }
        }

        /**
         * 查询全部微信用户
         */
        @GetMapping("/allwxuser")
        private  ResponseEntity<Result> selectAllWxuser(){
                try{
                        List<wxuser> wxusers =wxuserService.select();
                        return new ResponseEntity<>(new Result(200 ,null,wxusers),HttpStatus.OK);
                }catch (Exception e){
                        e.printStackTrace();
                        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }

        }

}
